<template>
  <!-- 注册页面的整体盒子 -->
  <div class="register-container">
    <!-- 注册的盒子 -->
    <div class="register-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form ref="regFormRef" :model="regForm" :rules="regFormRules">
        <el-form-item prop="username">
          <el-input v-model.trim="regForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user-solid">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model.trim="regForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            type="password"
            v-model.trim="regForm.repassword"
            placeholder="请再次输入密码"
            prefix-icon="el-icon-lock"
            show-password
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="headleReg">立即注册</el-button>
        </el-form-item>
        <el-link type="info" @click="$router.replace('/login')">去登录</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
// 导入注册成功后调用接口的文档
import { registerAPI } from '@/api/Register.js'
export default {
  name: 'Register',
  data() {
    // 自定义校检第一次输入密码
    const validatePass = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      // 表单验证
      regFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-z\d]{2,10}$/i, message: '用户名必须是2-10的字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          // 确认密码需要和密码一致，需要借助于自定义校检
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 表单的兜底校检
    headleReg() {
      this.$refs.regFormRef.validate(async vaild => {
        if (!vaild) return
        console.log('ok')
        // 通过后，就可以发起请求了
        // // 1. 发起注册请求
        const { data: res } = await registerAPI(this.regForm)
        console.log(res)
        // 2. 注册失败，提示用户
        if (res.code !== 0) return this.$message.error(res.message)
        // // 3. 注册成功，提示用户
        this.$message.success(res.message)
        // // 4. 跳转到登录页面
        this.$router.replace('/login')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  // 在样式中使用src目录下的图片需要在@前边加一个 “ ~ ”才可以
  background: url('~@/assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .register-box {
    box-sizing: border-box;
    width: 400px;
    height: 360px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    .title-box {
      height: 60px;
      background: url('~@/assets/images/login_title.png') center no-repeat;
    }
  }
  .el-button {
    width: 100%;
  }
}
</style>
